<?php

class logisticstrack_service_hqepay {

    function __construct() {
        $this->app = app::get('logisticstrack');
    }

    // 绑定华强宝物流
    public function bind() {
        base_kvstore::instance('ome/bind/hqepay')->store('ome_bind_hqepay', true);
        return true;
    }

    public function genSign($params, $token) {
        ksort($params);
        $str = '';
        foreach ($params as $key => $value) {
            if ($key != 'certi_ac') {
                $str .= $value;
            }
        }
        $signString = md5($str . $token);
        return $signString;
    }

    public function rpc_logistics_hqepay($rpc_data) {
        base_kvstore::instance('ome/bind/hqepay')->fetch('ome_bind_hqepay', $is_ome_bind_hqepay);
        if (! $is_ome_bind_hqepay) {
            $rs = $this->bind();
            if (! $rs) {
                $return_data['rsp'] = 'fail';
                $return_data['err_msg'] = '没有绑定!';
                return $return_data;
            }
        }
        
        $return_data['rsp'] = 'succ';
        $return_data['data'] = array();
        return $return_data;
    }

    function gen_matrix_sign($params, $token) {
        return strtoupper(md5(strtoupper(md5($this->assemble($params))) . $token));
    }

    function assemble($params) {
        if (! is_array($params)) {
            return null;
        }
        
        ksort($params, SORT_STRING);
        $sign = '';
        foreach ($params as $key => $val) {
            $sign .= $key . (is_array($val) ? assemble($val) : $val);
        }
        return $sign;
    }
}
